package com.example.springstudy.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.example.springstudy.dao.dto.EmployeeDto;
import com.example.springstudy.dao.res.EmployeeListRes;
import com.example.springstudy.entity.Dept;
import com.example.springstudy.entity.Employee;
import com.example.springstudy.entity.User;
import com.example.springstudy.mapper.EmployeeMapper;
import com.example.springstudy.service.DeptService;
import com.example.springstudy.service.EmployeeService;
import com.example.springstudy.service.UserService;
import com.example.springstudy.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
public class EmployeeServiceImp extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {

    @Autowired
    EmployeeMapper employeeMapper;
    @Autowired
    UserService userService;
    @Autowired
    DeptService deptService;

    @Override
    public EmployeeListRes getAllEmployees(int page, int size) {
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<Employee>();
        int total = (int) this.count(wrapper.eq(Employee::getIsDeleted, 0));
        List<EmployeeDto> list = employeeMapper.listEmployee();
        return new EmployeeListRes(list, page, size, total);
    }

    @Transactional
    @Override
    public boolean saveEmployee(EmployeeDto employeeDto) {
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Employee::getId).last("limit 1");
        Employee last = this.list(wrapper).get(0);

        Dept dept = deptService.findOne(employeeDto.getDeptId());
        int cardNo = last.getId()+1;
        String cardNoFormat = String.format("%03d", cardNo);
        String newCode = dept.getCode()+cardNoFormat;
        employeeDto.setEmployeeNo(newCode);

        User user = toUser(employeeDto);
        boolean success = userService.addUser(user);
        if(success){
            Employee employee = toEmployee(employeeDto, user.getId());
            employee.setCreateTime(DateUtils.defaultDate());
            return this.save(employee);
        }
        return false;
    }

    @Override
    public boolean updateEmployee(EmployeeDto employeeDto) {
        boolean saveUser = userService.updateUser(toUser(employeeDto));
        if(saveUser){
            return this.updateById(toEmployee(employeeDto, employeeDto.getUserId()));
        }
        return false;
    }

    private Employee toEmployee(EmployeeDto dto, Long userId){
        Employee employee = new Employee();
        if(dto.getId()!=null && dto.getId()!=0){
            employee.setId(dto.getId());
        }
        employee.setUserId(userId);
        employee.setName(dto.getEmployeeName());
        employee.setDeptId(dto.getDeptId());
        employee.setJobId(dto.getJobId());
        employee.setCardNo(dto.getEmployeeNo());
        employee.setDataScope(dto.getDataScope());
        return employee;
    }

    private User toUser(EmployeeDto dto){
        User user = new User();
        user.setId(dto.getUserId()==0?null:dto.getUserId());
        user.setPassword(StrUtil.isEmpty(dto.getPassword()) ? null:dto.getPassword());
        user.setPhone(dto.getPhone());
        user.setName(dto.getEmployeeNo());
        user.setNickName(dto.getEmployeeName());
        user.setDeptId(dto.getDeptId());
        user.setHeight(dto.getHeight());
        user.setWeight(dto.getWeight());
        user.setBirth(dto.getBirth());
        user.setAddress(dto.getAddress());
        user.setRegion(dto.getRegion());
        user.setAvatar(dto.getAvatar());
        user.setEmail(dto.getEmail());
        user.setIdcard(dto.getIdcard());
        user.setSex(dto.getSex());
        return user;
    }
}
